﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web.Profile;

namespace LULSMS
{
    public partial class Patients
    {

        public static List<LULSMS_Lib.User> getSpecialistActivePatients(string userName, string password){
               try
            {
                List<LULSMS_Lib.User> aPatients = new List<LULSMS_Lib.User>();
                   //Authenticate
                using (LUL_DBEntities entity = new LUL_DBEntities())
                {
                
                    var sel = from conect in entity.UserConnection
                             where conect.UserID.Equals(conect.aspnet_Users.Patients.UserID) && conect.EndDate==null
                                  select new LULSMS_Lib.User(){
                                      UserName = conect.aspnet_Users.UserName,
                                      IP = conect.IP
                                      
                                  };

                             
                             
                    aPatients = sel.ToList<LULSMS_Lib.User>();

                    aspnet_Users sp = entity.aspnet_Users.FirstOrDefault(i => (i.UserName.Equals(userName)));
                    //TODO: Utilizar SP
                    
                }
                return aPatients;
            }
            catch (Exception ex)
            {
                //TODO: Tratar excepções
                return new List<LULSMS_Lib.User>();
            }
        }
        

        public static List<LULSMS_Lib.User> getSpecialistPatients(string userName, string password)
        {
            
            return new List<LULSMS_Lib.User>();
        }

        public static List<LULSMS_Lib.User> getActivePatients(string userName, string password)
        {
            try
            {
                List<LULSMS_Lib.User> aPatients = new List<LULSMS_Lib.User>();
                //Authenticate
                using (LUL_DBEntities entity = new LUL_DBEntities())
                {

                    var sel = from conect in entity.UserConnection
                              where conect.UserID.Equals(conect.aspnet_Users.Patients.UserID) && conect.EndDate == null
                              select new LULSMS_Lib.User()
                              {
                                  UserName = conect.aspnet_Users.UserName,
                                  IP = conect.IP

                              };



                    aPatients = sel.ToList<LULSMS_Lib.User>();

                    for (int i = 0; i < aPatients.Count ;i++)
                    {
                        ProfileBase pb = ProfileBase.Create(aPatients[i].UserName);
                        aPatients[i].CitizenNumber = (int)pb.GetPropertyValue("CitizenNumber");
                        aPatients[i].FullName = (string)pb.GetPropertyValue("FullName");
                    }

                    aspnet_Users sp = entity.aspnet_Users.FirstOrDefault(i => (i.UserName.Equals(userName)));
                    //TODO: Utilizar SP

                }
                return aPatients;
            }
            catch (Exception ex)
            {
                //TODO: Tratar excepções
                return new List<LULSMS_Lib.User>();
            }
            return new List<LULSMS_Lib.User>();
        }

        public static List<LULSMS_Lib.User> getPatients(string userName, string password)
        {
            return new List<LULSMS_Lib.User>();
        }

        public static LULSMS_Lib.User getPatient(string patient, string userName, string password)
        {
            return new LULSMS_Lib.User();
        }
    }
}
